package utils

import (
	"context"
	"github.com/mojocn/base64Captcha"
	log "go-micro.dev/v4/logger"
	"xm_shop/database"
	. "xm_shop/models"
	. "xm_shop/proto/captcha"
)

// 创建store(验证码存储的地方)
//var store = base64Captcha.DefaultMemStore

// 采用redis存储
var store base64Captcha.Store = database.RedisStore{}

// 获取验证码
func MakeCaptcha(height int, width int, length int) (string, string, string, error) {
	// 创建调用对象
	captchaClient := NewCaptchaService("captcha", MicroClient)
	res, err := captchaClient.MakeCaptcha(context.Background(), &MakeCaptchaRequest{
		Height: int32(height),
		Width:  int32(width),
		Length: int32(length),
	})
	if err != nil {
		log.Fatal(err)
	}
	return res.Id, res.B64S, "", err
}

// 验证验证码
func VerifyCaptcha(id string, VerifyValue string) bool {
	captchaClient := NewCaptchaService("captcha", MicroClient)
	res, err := captchaClient.VerifyCaptcha(context.Background(), &VerifyCaptchaRequest{
		Id:          id,
		VerifyValue: VerifyValue,
	})
	if err != nil {
		return false
	}
	return res.GetVerifyResult()
}
